﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using UnityEngine.UI;
using DG.Tweening;
using System;
using Beyond.Ant;

/// <summary>
/// 拖拽UI
/// </summary>
public class Drag : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
    private RectTransform parentRTF;
    Vector3 v2;
    public List<Image> imageList = new List<Image>();
    public List<Vector3> v3 = new List<Vector3>();
    Vector2 downPos;
    public GameObject back;
    [Header("图片之间的间隔")]
    public float dragInterval;
    [Header("拖动到下一个图片的距离判断")]
    public float judgePageLength;
    private void Start()
    {
        parentRTF = this.transform.parent as RectTransform;
        v2 = transform.position;

        dragInterval = v3[1].x - v3[0].x;
        if (GameState.InpStyle == InPromotion.Style.FullLandscape)
            judgePageLength = 450;
        else if (GameState.InpStyle == InPromotion.Style.FullPortrait)
            judgePageLength = 3.2f;
    }

    void Update()
    {
        if (GameState.index == -1)
            GameState.index = imageList.Count - 1;

        if (GameState.index == imageList.Count)
            GameState.index = 0;

        if (transform.GetChild(imageList.Count - 1).position.x > v3[imageList.Count - 1].x)
        {
            transform.GetChild(imageList.Count - 1).position = new Vector3(transform.GetChild(imageList.Count - 1).position.x - dragInterval * imageList.Count, v2.y, v2.z);
            transform.GetChild(imageList.Count - 1).SetAsFirstSibling();
        }

        if (transform.GetChild(imageList.Count - 1).position.x < v3[0].x)
        {
            transform.GetChild(0).position = new Vector3(transform.GetChild(0).position.x + dragInterval * imageList.Count, v2.y, v2.z);
            transform.GetChild(0).SetAsLastSibling();
        }

        if (Input.GetKeyDown(KeyCode.E))
            RightEvent();
        if (Input.GetKeyDown(KeyCode.Q))
            LeftEvent();
    }

    //按下点 到 轴心点 偏移量
    private Vector3 pressToPivotOffset;
    //光标按下时执行
    public void OnPointerDown(PointerEventData eventData)
    {
        if (NewInPromotions.Instance.InPromotionList.Count != 1)
        {
            //将按下点的屏幕坐标 转换为 世界坐标
            Vector3 worldPosition;
            RectTransformUtility.ScreenPointToWorldPointInRectangle
                (parentRTF, eventData.position, eventData.pressEventCamera, out worldPosition);

            pressToPivotOffset = this.transform.position - worldPosition;

            downPos = transform.position;
            print(downPos);
        }
        else
        {
            print("点击");
            NewInPromotions.Instance.PictureEvent(GameState.index);
        }
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (NewInPromotions.Instance.InPromotionList.Count != 1)
        {
            Vector3 world;
            RectTransformUtility.ScreenPointToWorldPointInRectangle
                (parentRTF, eventData.position, eventData.pressEventCamera, out world);

            this.transform.position = world + pressToPivotOffset;
            transform.position = new Vector3(transform.position.x, v2.y, 0);
        }
    }


    public void OnPointerUp(PointerEventData eventData)
    {
        if (NewInPromotions.Instance.InPromotionList.Count != 1)
        {
            print(transform.position);
            float DiffValue = transform.position.x - downPos.x;
            if (DiffValue == 0)
            {
                print("点击");
                NewInPromotions.Instance.PictureEvent(GameState.index);
                return;
            }

            back.gameObject.SetActive(true);
            if (DiffValue > 0)
            {
                print("右移");
                if (DiffValue > judgePageLength)
                {
                    ChangeIndex(Direction.Right);
                    transform.DOMoveX(downPos.x + dragInterval, 0.3f).OnComplete(() => { back.gameObject.SetActive(false); });

                }
                else
                    transform.DOMoveX(downPos.x, 0.5f).OnComplete(() => { back.gameObject.SetActive(false); });
            }

            else if (DiffValue < 0)
            {
                print("左移");
                if (DiffValue < -judgePageLength)
                {
                    ChangeIndex(Direction.Left);
                    transform.DOMoveX(downPos.x - dragInterval, 0.3f).OnComplete(() => { back.gameObject.SetActive(false); });

                }
                else
                    transform.DOMoveX(downPos.x, 0.5f).OnComplete(() => { back.gameObject.SetActive(false); });
            }
        }

    }

    public void ChangeIndex(Direction dirType)
    {
        if (dirType == Direction.Left)
        {
            GameState.index++;
        }

        else if (dirType == Direction.Right)
        {
            GameState.index--;
        }

    }

    bool allowPoint = true;
    public void RightEvent()
    {
        if (NewInPromotions.Instance.InPromotionList.Count != 1)
        {
            if (allowPoint)
            {
                allowPoint = false;
                back.SetActive(true);
                ChangeIndex(Direction.Right);
                transform.DOMoveX(transform.position.x + dragInterval, 1f).OnComplete(() => { CompleteEvent(); });
            }
        }
    }

    public void LeftEvent()
    {
        if (NewInPromotions.Instance.InPromotionList.Count != 1)
        {
            if (allowPoint)
            {
                allowPoint = false;
                back.SetActive(true);
                ChangeIndex(Direction.Left);
                transform.DOMoveX(transform.position.x - dragInterval, 1f).OnComplete(() => { CompleteEvent(); });
            }
        }
    }


    public void CompleteEvent()
    {
        print("complete");
        allowPoint = true;
        back.SetActive(false);
    }
    public enum Direction
    {
        Left,
        Right,
    }

}
